using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Woz
{
    /// <summary>
    /// ungroup action groups list of entities into one entity called group
    /// </summary>
    internal class UngroupAction : Woz.Action
    {
        /// <summary>
        /// creates an instance of the ungroup acton
        /// </summary>
        /// <param name="pEntities">the group to ungroup</param>
        public UngroupAction(Woz.Group pGroup)
        {
            // save the group
            this.mGroup = pGroup;

            // make action string
            this.mActionString = "Ungrouped Entities";
        }

        /// <summary>
        /// gets the name of the tool this action prefers
        /// </summary>
        public override string ToolName
        {
            get
            {
                return "Select";
            }
        }

        /// <summary>
        /// gets whether this action can be propagated or not
        /// </summary>
        public override bool Propagatable
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// ungroups the selected entities
        /// </summary>
        /// <param name="pScreen">screen to ungroup entities on</param>
        protected override void OnExecute(Woz.Screen pScreen)
        {
            // remove the group from the screen
            pScreen.DeselectEntity(this.mGroup, false);
            pScreen.RemoveEntity(this.mGroup, false);

            // put the entities on the screen
            foreach (Woz.Entity entity in this.mGroup.Entities)
            {
                pScreen.AddEntity(entity, false);
                pScreen.SelectEntity(entity, false);
            }

            // now request the screen to update the bounding box
            Woz.ScreenManager.Instance.RequestSelectedScreenUpdate(pScreen, 
                this.mGroup.BoundingBox);
        }

        /// <summary>
        /// groups the selected entities
        /// </summary>
        /// <param name="pScreen">screen to group entities on</param>
        protected override void OnUnExecute(Woz.Screen pScreen)
        {
            // remove the entities from the screen
            foreach (Woz.Entity entity in this.mGroup.Entities)
            {
                pScreen.DeselectEntity(entity, false);
                pScreen.RemoveEntity(entity, false);
            }

            // put the group on the screen
            pScreen.AddEntity(this.mGroup, false);
            pScreen.SelectEntity(this.mGroup, false);

            // now request the screen to update the bounding box
            Woz.ScreenManager.Instance.RequestSelectedScreenUpdate(pScreen, 
                this.mGroup.BoundingBox);
        }

        /// <summary>
        /// check to see if this action can be applied to the given screen
        /// </summary>
        /// <param name="pScreen">the screen to check</param>
        /// <returns>true if the action can be applied to the given screen</returns>
        public override bool CanApplyTo(Screen pScreen)
        {
            // make sure all entities are contained in this screen
            foreach (Woz.Entity entity in this.mGroup.Entities)
            {
                if (!pScreen.Contains(entity))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// the group to group entities to
        /// </summary>
        private Woz.Group mGroup;
    }
}
